package com.ylbh.modules.goods.controller;

import com.ylbh.common.annotation.SysLog;
import com.ylbh.common.exception.NormalException;
import com.ylbh.common.pojo.goods.dto.*;
import com.ylbh.common.pojo.goods.vo.GoodsDetailsVO;
import com.ylbh.common.pojo.goods.vo.GoodsQueryVO;
import com.ylbh.common.pojo.goods.vo.SpecIficationVO;
import com.ylbh.common.utils.*;
import com.ylbh.modules.goods.service.GoodsSpecService;
import com.ylbh.modules.goods.service.ShoppingGoodsService;
import com.ylbh.modules.sys.controller.AbstractController;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;


/**
 *
 * @author czx
 * @date 2019-04-23 15:17:38
 */
@RestController
@RequestMapping("goods")
public class ShoppingGoodsController extends AbstractController {
    @Autowired
    private ShoppingGoodsService shoppingGoodsService;
    @Autowired
    private GoodsSpecService goodsSpecService;

    @Value("${url.shoppingMainImage}")
    private String uploadHost;

    @ApiOperation("商城商品信息列表")
    @RequestMapping("queryGoodsList")
    @RequiresPermissions("goods:queryGoodsList")
    public CommonResult<PageUtils> queryGoodsList(GoodsListDTO goodsListDTO){
        PageUtils utils = shoppingGoodsService.queryGoodsList(goodsListDTO);
        return Ref.ok(utils);
    }

    @ApiOperation("商城商品信息详情")
    @RequestMapping("getGoodsById/{id}")
    @RequiresPermissions("goods:getGoodsById")
    public CommonResult<GoodsDetailsVO> getGoodsById(@PathVariable(value = "id")Long id){
        GoodsDetailsVO vo = shoppingGoodsService.getGoodsById(id);
        return Ref.ok(vo);
    }

    @ApiOperation("修改商城商品信息详情")
    @SysLog("修改商城商品信息详情")
    @RequestMapping("modifyGoodsById")
    @RequiresPermissions("goods:modifyGoodsById")
    public CommonResult<String> modifyGoodsById(@RequestBody GoodsDTO updateGoodsDetailsDTO){
        return shoppingGoodsService.modifyGoodsById(updateGoodsDetailsDTO, ShiroUtils.getUserId());
    }

    @ApiOperation("修改商城商品SKU信息详情")
    @SysLog("修改商城商品SKU信息详情")
    @RequestMapping("modifySkuById")
    @RequiresPermissions("goods:modifyGoodsById")
    public CommonResult<Void> modifySkuById(@RequestBody GoodsDetailsDTO updateGoodsDetailsDTO){
        shoppingGoodsService.modifySkuById(updateGoodsDetailsDTO);
        return Ref.ok();
    }


    /**
     * 批量商品修改状态
     */
    @ApiOperation("批量商品修改状态")
    @SysLog("批量商品修改状态")
    @RequestMapping("/batchModifyState/{type}")
    @RequiresPermissions("goods:batchModifyState")
    public CommonResult batchHandle(@PathVariable("type")String type,
                         @RequestBody Integer[] ids){
        shoppingGoodsService.batchModifyState(type,ids);
        return Ref.ok();
    }

    /**
     * 通过分类id获取分类对应的规格数据
     */
    @ApiOperation("通过分类id获取分类对应的规格数据")
    @RequestMapping("/querySpecsList/{gcId}")
    public CommonResult<List<SpecIficationVO>> querySpecsList(@PathVariable("gcId")Integer gcId){
        List<SpecIficationVO> list = goodsSpecService.querySpecsList(gcId);
        return Ref.ok(list);
    }

    /**
     * 上传商品
     */
    @SysLog("上传商品")
    @ApiOperation("上传商品")
    @RequestMapping("/uploadGoods")
    public CommonResult uploadGoods(@RequestBody GoodsDTO goodsDTO){
        shoppingGoodsService.add(goodsDTO,getUserId());
        return Ref.ok();
    }

    /**
     * 上传图片
     */
    @ApiOperation("上传商品图片")
    @RequestMapping("/uploadImg")
    public CommonResult<String> uploadImg(@RequestBody MultipartFile file){
        if(file==null){
            return Ref.error("上传图片时，图片为空");
        }
        OSSClient ossClient = new OSSClient();

        try {
            String suffix = "33307/"+DateHelper.getDate();
            String name = "";
            String suffixName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
            if ("jpg".equals(suffixName) || "JPG".equals(suffixName) || "PNG".equals(suffixName) || "png".equals(suffixName)) {
                name = ossClient.uploadCompressImg2Oss(file, "consumer/upload/store/", suffix);
            } else {
                name = ossClient.uploadVideo2Oss(file, "consumer/upload/store/", suffix);
            }
            String url = uploadHost+"upload/store/"+suffix+"/"+name;
            return Ref.ok(url);
        } catch (Exception e) {
            e.printStackTrace();
            return Ref.error("上传图片失败");
        }
    }

    /**
     * 添加自定义规格
     */
    @ApiOperation("添加自定义规格")
    @SysLog("添加自定义规格")
    @RequestMapping("/addCustomSpecs")
    public CommonResult<SpecIficationDTO> addCustomSpecs(SpecIficationDTO specIficationDTO){
        specIficationDTO = goodsSpecService.addCustomSpecs(specIficationDTO);
        return Ref.ok(specIficationDTO);
    }

    /**
     * 通过属性查询自定义规格
     */
    @ApiOperation("通过属性查询自定义规格")
    @RequestMapping("/getCustomSpecs/{id}")
    public CommonResult<SpecIficationVO> getCustomSpecs(@PathVariable Long id){
        SpecIficationVO customSpecs = goodsSpecService.getCustomSpecs(id);
        return Ref.ok(customSpecs);
    }

    @ApiOperation("商城商品导出")
    @RequestMapping("goodsExcel")
    public CommonResult<Void> goodsExcel(HttpServletResponse response, HttpServletRequest request) throws NormalException {
        shoppingGoodsService.goodsExcel(response, request);
        return Ref.ok();
    }

    @SysLog("修改商品的排序")
    @ApiOperation("修改商品的排序")
    @RequestMapping("modifyGoodsSortOrder/{id}/{sortOrder}")
    public CommonResult<Void> modifyGoodsSortOrder(@PathVariable Long id, @PathVariable Integer sortOrder) {
        GoodsDTO goods = new GoodsDTO();
        goods.setId(id);
        goods.setSortOrder(sortOrder);
        shoppingGoodsService.updateById(goods);
        return Ref.ok();
    }

//    @PostMapping("modifyPriceType")
//    @ApiOperation("修改兑换商品的运费(禁用）")
//    public CommonResult modifyPriceType() {
//        shoppingGoodsService.modifyPriceType();
//        return Ref.ok();
//    }

    @GetMapping("queryGoodsImages")
    @ApiOperation("查询所有商品的图片地址")
    public CommonResult<List<GoodsQueryVO>> queryGoodsImages() {
        List<GoodsQueryVO> vo = shoppingGoodsService.queryGoodsImages();
        return Ref.ok(vo);
    }

    @GetMapping("getIsActivityGoodsById/{id}")
    @ApiOperation("在列表查看对应sku查询是否已经是活动商品")
    public CommonResult<GoodsDetailsVO> getIsActivityGoodsById(@PathVariable Long id) {
        GoodsDetailsVO vo = shoppingGoodsService.getIsActivityGoodsById(id);
        return Ref.ok(vo);
    }

    @ApiOperation("查看对应商品评论")
    @GetMapping("getGoodsCommentById")
    public CommonResult getGoodsCommentById(PageDTO pageDTO) {
        PageUtils page = shoppingGoodsService.getGoodsCommentById(pageDTO);
        return Ref.ok(page);
    }

    @SysLog("删除商品评论")
    @ApiOperation("删除商品评论")
    @PostMapping("removeGoodsCommentByIds")
    public CommonResult removeGoodsCommentByIds(@RequestBody String[] ids) {
        shoppingGoodsService.removeGoodsCommentByIds(Arrays.asList(ids));
        return Ref.ok();
    }

    @GetMapping("getGoodsCount")
    public CommonResult getGoodsCount() {
        shoppingGoodsService.getGoodsCount();
        return Ref.ok();
    }

    @ApiOperation("计算商品的利润")
    @GetMapping("modifyGoodsCostingPrice")
    public CommonResult modifyGoodsCostingPrice() {
        shoppingGoodsService.modifyGoodsCostingPrice();
        return Ref.ok();
    }

    @ApiOperation("解析数据")
    @PostMapping("analyticalData")
    public CommonResult analyticalData(@RequestBody PoiZonGoodsDTO dto) {
        GoodsDetailsVO vo = shoppingGoodsService.analyticalData(dto);
        return Ref.ok(vo);
    }

    @ApiOperation("上传商品详情图片")
    @PostMapping("uploadGoodsDetails")
    public CommonResult uploadGoodsDetails(@RequestBody GoodsDTO goodsDTO) {
        String content = shoppingGoodsService.uploadGoodsDetails(goodsDTO);
        return Ref.ok(content);
    }

    @ApiOperation("上传商品验证型号是否已存在")
    @GetMapping("verificationGoodsSerial/{goodsSerial}")
    public CommonResult verificationGoodsSerial(@PathVariable String goodsSerial) {
        int count = shoppingGoodsService.verificationGoodsSerial(goodsSerial);
        return Ref.ok(count);
    }
}
